var _Scene = require('component/scene.js');
var _layout = require('tools/layout.js');
var _relative = _layout.relative;
var _FloatView = require('widget/floatview.js');
var _inherit = require('core/inherit.js');
var _mapconf = require('game/map/config.js');
var _Map = require('scenes/maps/map.js');

/**
 * slice is y direction
 *
 * @param view
 * @constructor
 */
function BgView(view) {
    _FloatView.call(this, view);
}
_inherit(BgView, _FloatView);
BgView.prototype.moving = function (current) {
    current.sub(this.mDown);
    this.mVector.set(this.mDownPos);
    this.mVector.add(current);
    this.mVector[0] = 0;
    var y = this.mVector[1];
    if (y > 0) {
        y = 0;
    } else {
        var max = this.mInner.height() - this.height();
        if (y < -max) {
            y = -max;
        }
    }
    this.mVector[1] = y;
    this.mInner.setPosition(this.mVector);
};

/**
 * scale wrapper
 * @type {*}
 */
var GameSelect = _Scene.createScene(
    function (w, h) {
        var map = new _Map(_mapconf[0]);
        this.mMapSize = map.getSize();
        this.addChild(this.mBg = new BgView(map));
        this.querySelector('map>button').forEach(function (button) {
            console.log('regist buttons', button);
            button.on('click', this.startLevel, this);
        }, this);

        this.onSizeChange(w, h);
    }
);
GameSelect.prototype.update = function (context) {
};
GameSelect.prototype.startLevel = function (button) {
    console.log('GameSelect.prototype.startLevel:' + button);
};
GameSelect.prototype.onSizeChange = function (w, h) {
    this.setSize(w, h);
    var scale = w / this.mMapSize[0];
    var mapHeight = h / scale;
    this.mBg.setSize(this.mMapSize[0], mapHeight);
    this.mBg.setScale(scale);
    _relative.local.layoutTo(this.mBg, 0, 0, this, 0, 0);
};

module.exports = GameSelect;